package com.study.blogsearchconsumer.rabbitMQ;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;

public class MqProperties {
    public MqProperties() {
    }

    public static ConnectionFactory getConnectionFactory(String address, String userName, String password, String virtualHost) {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setAddresses(address);
        connectionFactory.setUsername(userName);
        connectionFactory.setPassword(password);
        connectionFactory.setVirtualHost(virtualHost);
        connectionFactory.setPublisherConfirms(true);
        return connectionFactory;
    }

    public static RabbitTemplate getRabbitTemplate(ConnectionFactory connectionFactory) {
        return new RabbitTemplate(connectionFactory);
    }

    public static RabbitAdmin getRabbitAdmin(ConnectionFactory connectionFactory) {
        return new RabbitAdmin(connectionFactory);
    }

    public static FanoutExchange getFanoutExchange(RabbitAdmin rabbitAdmin, String exChangeName) {
        FanoutExchange exchange = new FanoutExchange(exChangeName);
        exchange.setAdminsThatShouldDeclare(new Object[]{rabbitAdmin});
        rabbitAdmin.declareExchange(exchange);
        return exchange;
    }

    public static DirectExchange getDirectExchange(RabbitAdmin rabbitAdmin, String exChangeName) {
        DirectExchange exchange = new DirectExchange(exChangeName);
        exchange.setAdminsThatShouldDeclare(new Object[]{rabbitAdmin});
        rabbitAdmin.declareExchange(exchange);
        return exchange;
    }

    public static TopicExchange getTopicExchange(RabbitAdmin rabbitAdmin, String exChangeName) {
        TopicExchange exchange = new TopicExchange(exChangeName);
        exchange.setAdminsThatShouldDeclare(new Object[]{rabbitAdmin});
        rabbitAdmin.declareExchange(exchange);
        return exchange;
    }

    public static Queue getQueue(RabbitAdmin rabbitAdmin, String queueName) {
        Queue queue = new Queue(queueName, true);
        queue.setAdminsThatShouldDeclare(new Object[]{rabbitAdmin});
        rabbitAdmin.declareQueue(queue);
        return queue;
    }

    public static Binding bindFanoutExchange(RabbitAdmin rabbitAdmin, FanoutExchange exchange, Queue queue) {
        Binding binding = BindingBuilder.bind(queue).to(exchange);
        binding.setAdminsThatShouldDeclare(new Object[]{rabbitAdmin});
        rabbitAdmin.declareBinding(binding);
        return binding;
    }

    public static Binding bindDirectExchange(RabbitAdmin rabbitAdmin, DirectExchange exchange, Queue queue, String routingKey) {
        Binding binding = BindingBuilder.bind(queue).to(exchange).with(routingKey);
        binding.setAdminsThatShouldDeclare(new Object[]{rabbitAdmin});
        rabbitAdmin.declareBinding(binding);
        return binding;
    }

    public static Binding bindTopicExchange(RabbitAdmin rabbitAdmin, TopicExchange exchange, Queue queue, String routingKey) {
        Binding binding = BindingBuilder.bind(queue).to(exchange).with(routingKey);
        binding.setAdminsThatShouldDeclare(new Object[]{rabbitAdmin});
        rabbitAdmin.declareBinding(binding);
        return binding;
    }
}